package com.zxm.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.zxm.pojo.UserInfo;
import com.zxm.result.EasyUiResult;
import com.zxm.result.GuhealthResult;
import com.zxm.service.UserInfoService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.Date;
import java.util.Map;

@Controller
@RequestMapping("/userInfo")
public class UserInfoController {
	@Reference
	UserInfoService userInfoService;
	//用户查看个人信息
	@RequestMapping("/showUserInfo")
	@ResponseBody
	public UserInfo showUserInfo(UserInfo userInfo){
		return userInfoService.showUserInfo(userInfo);
	}
	
	//用户更改个人信息（根据ID有选择性的更改）
	@RequestMapping("/modifyUserInfo")
	@ResponseBody
	public GuhealthResult modifyUserInfo(UserInfo userInfo) {
		if(userInfo!=null){
			userInfoService.modifyUserInfo(userInfo);
			return GuhealthResult.success();
		}
		return GuhealthResult.fail();
	}
	//用户注册（添加用户）
	@RequestMapping("/addUserInfo")
	@ResponseBody
	public GuhealthResult addUserInfo(UserInfo userInfo) {
		boolean flag = userInfoService.checkUsername(userInfo.getUsername());
		if(flag){
			int i = userInfoService.insertUserInfo(userInfo);
			if(i>0){
				return GuhealthResult.success();
			}
		}
		return GuhealthResult.fail();
	}
	//分页查询
	@RequestMapping("/showUserInfoByPage")
	@ResponseBody
	public EasyUiResult showUserInfoByPage(@RequestParam(defaultValue="1") Integer page,
			@RequestParam(defaultValue="15") Integer rows) {
		return userInfoService.selectByPage(page, rows);
	}
	//用户创建的时候，检验数据库中是否已经存在这个用户名
	@RequestMapping("/checkUsername")
	@ResponseBody
	public GuhealthResult checkUsername(UserInfo userInfo) {
		Boolean flag=userInfoService.checkUsername(userInfo.getUsername());
		if(flag) {
			return GuhealthResult.success();
		}
		return GuhealthResult.fail();
	}
	/**
	 * 查看十天用户的增量
	 */
	@RequestMapping("/getAddUserNumByTenDays")
	@ResponseBody
	public Map<String,Integer> getAddUserNumByTenDays(String date){
		Date date1 = Date.valueOf(date);
		return userInfoService.getUserInfoInTenDa(date1);
	}
	/**
	 * 查询各个年龄段中用户的数量
	 */
	@RequestMapping("/getAgeOfAllUser")
	@ResponseBody
	public Map<Integer,Integer> getAgeOfAllUser(){
		return userInfoService.getAgeOfAllUser();
	}
	/**
	 * 查询用户中城市人数的分布
	 */
	@RequestMapping("/getAareOfAllUser")
	@ResponseBody
	public Map<String,Integer> getAareOfAllUser(){
		return userInfoService.getAareOfAllUser();
	}

	/**
	 * 查询所有用户的性别比例
	 */
	@RequestMapping("/analySexNumOfUser")
	@ResponseBody
	public Map<String,Integer> analySexNumOfUser(){
		return userInfoService.analySexNumOfUser();
	}

}